/**
 * @author yxw
 * date : 2013年11月18日 下午7:28:00 
 */
package com.tzinfo.tzt.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.util.MyApplication;
import com.tzinfo.tzt.activity.view.ImageAdapter;
import com.tzinfo.tzt.domain.NewhouseBuyInfo;

public class MyTuanActivity extends BaseActivity implements OnClickListener,
		OnItemClickListener {

	// 公共部分
	private MyApplication mApp;
	private Button backBtn;
	private ListView listView;
	private ImageAdapter imageAdapter;
	private TextView titleTextView;
	private RelativeLayout noCllectionLayout, cllectionListLayout;

	private List<Map<String, Object>> data;// 二手房数据list
	private List<NewhouseBuyInfo> tuanList;

	// 标志
	public String sHouseId;

	static String sID = "sID";
	static String sTime = "Time";
	static String sTitle = "Title";
	static String sLogo = "Logo";
	static String sName = "Name";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_tuan);
		init();
		getTuanData();
	}

	private void init() {
		mApp = (MyApplication) getApplication();
		mApp.setExit(false);
		backBtn = (Button) this.findViewById(R.id.bt_back);
		backBtn.setOnClickListener(this);

		titleTextView = (TextView) this.findViewById(R.id.text_title);
		titleTextView.setText(getResources().getString(R.string.mypage_grid04));

		listView = (ListView) this.findViewById(R.id.listView);
		listView.setOnItemClickListener(this);
		noCllectionLayout = (RelativeLayout) this
				.findViewById(R.id.layout_no_collection);
		cllectionListLayout = (RelativeLayout) this
				.findViewById(R.id.layout_collection_list);

	}

	private void getTuanData() {
		tuanList = tuanService.getNewhouseBuyInfoList();
		data = new ArrayList<Map<String, Object>>();
		if (tuanList != null && tuanList.size() > 0) {
			for (int i = 0; i < tuanList.size(); i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				NewhouseBuyInfo info = tuanList.get(i);
				map.put(sLogo, info.getPics().get(0));
				String strName = info.getName();
				if (strName.length() > 9) {
					strName = strName.substring(0, 9) + "…";
				}
				map.put(sName, strName);
				String strTitle = info.getSales();
				if (strTitle.length() > 14) {
					strTitle = strTitle.substring(0, 14) + "…";
				}
				map.put(sTitle, strTitle);
				map.put(sID, info.getHouseId());
				map.put(sTime, info.getRemainTime());
				data.add(map);
			}
			imageAdapter = new ImageAdapter(this, data,
					R.layout.item_tuan_list, new String[] { sLogo,sTitle, sName, sID, sTime }, new int[] {
							R.id.list_item_image, R.id.list_item_title,
							R.id.list_item_name, R.id.un_house_id,
							R.id.list_item_time});
			
			listView.setAdapter(imageAdapter);
			noCllectionLayout.setVisibility(View.GONE);
			cllectionListLayout.setVisibility(View.VISIBLE);
		} else {
			noCllectionLayout.setVisibility(View.VISIBLE);
			cllectionListLayout.setVisibility(View.GONE);
		}
	}

	protected boolean cleanDataByTab() {
		boolean flag = tuanService.deleteAll();
		Toast.makeText(MyTuanActivity.this, "数据已清空！", 5000).show();
		return flag;
	}

	protected boolean cleanDataById(String id) {
		boolean flag = false;
		flag = tuanService.deleteNewhouseBuyInfoId(id);
		return flag;
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getTuanData();
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.bt_back:
			this.finish();
			break;
		default:
			break;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> adapterview, View view, int i, long l) {
		TextView textView;
		String houseId;
		Intent intent = new Intent(this, TuangouDetailActivity.class);
		textView = (TextView) view.findViewById(R.id.un_house_id);
		houseId = textView.getText().toString();
		NewhouseBuyInfo item = tuanList.get((int)l);
		item.setHouseId(houseId);
		intent.putExtra("houseId", houseId);
		intent.putExtra("newhouseBuyInfo", item);
		startActivity(intent);
	}

}
